package SCH;
$Mojo::Webqq::Plugin::LCMD::PRIORITY = 1;

use Mojo::Ecrawler;

open $collegeData,'<','/home/lz/perlm/Mojo-Ecrawler/example/collegeData';
my @schurl=<$collegeData>;
close $collegeData;

my $rschool;
my $schoolinfo;

for(@schurl) {
next if /#^/;
next if /^\s*$/;
my ($sname,$surl)=split;

#print "$sname:$surl\n";
#print "$sname:$surl\n" if $surl;
$rschool->{$sname}=$surl if $surl;

}

#print Sseach($key);
#print $rschool->{$_},"$_ DDD\n" for(keys %{$rschool});
sub getScore {

my $result;

my $key=shift;

if (exists $schoolinfo->{$key}) {
$result=$schoolinfo->{$key} 
} else {

if (exists $rschool->{$key}) {

$result= Sccore($rschool->{$key});
$schoolinfo->{$rschool->{$key}}=$result;

} else {

for(keys %{$rschool}) {

$result.=" $_" if /$key/;

 }
}

}
return $result;
}

sub Sccore {

use Encode qw(decode encode);
use utf8;

my $WLI=0; # 文理科默认为理科，设置为1后是文科
my $provinceset=0; #定制特殊身份时候这个地方设置为1，并设置$provinceCode
my $provinceCode="10003"; # 省编码需要根据省专门设置,默认为高校所在省份

if($WLI){
 $lurl=~s#/10035/#/10034/#;

}

if($provinceset){

 my @url=split '/',$lurl;
 print "@url \n";
 $url[6]=$provinceCode if $provinceCode;
 my $llurl;
    $llurl.="$_/" for(@url);
    $llurl=~s#/$##;
    $lurl=$llurl;
}

my $DUBEG=0;
my $lurl=shift;
print "DEBUG $lurl" if $DUBEG;
my $pcontent = geturlcontent($lurl);
print "DEBUG $pcontent" if $DUBEG;

my $result=getdiv($pcontent, 'div.li-collegeHome',"ul.li-collegeInfo li div div");
my $score=getdiv($pcontent, 'div.places-tab',"table tr td");

my @sscore=split /\n/sm,$score;

print "DDXX ",$sscore[1],"XXDD" if $DUBEG;

my $score1="年份|最高分|平均分|最低分|省控线|批次\n";
$result="招办电话|电子邮箱|通讯地址|招生网址\n\n".$result;

#print "DD $_\n" for(@sscore);

for(@sscore){
#chomp;
#print "$i $_\n";
next if /span/;
s#<span.*##;
s#</span.*##;
s#(20\d\d)#\n\1年#;
#print "$i $_\n";
$score1.="|$_";
#$i++;
}

$result=encode 'utf8',$result;
$score1=encode 'utf8',$score1;

return "$result\n$score1\n";

}


sub call {
    my $client = shift;
    $client->on(receive_message=>sub{
        my($client,$msg)=@_;
        return if not $msg->allow_plugin;
        return if $msg->content !~ /(高校分数线|大学信息)/;
         my $key=$2 if $msg->content =~/(高校分数线|大学信息)\s+(.*)/;
         
         my $reply = getScore($key);
         #$reply=encode("utf8",$reply);
         
         $reply .="(源于Mojo-Webqq桔子高校分数线查询机器人Bata插件)" if $reply;
$msg->reply($reply,sub{$_[1]->from("bot")}) if $reply;
    
});

}
1;

